<?php
declare(strict_types=1);

namespace plugin\ky_black\app\admin\controller;

use support\Request;
use support\Response;
use plugin\ky_black\app\admin\model\Employee;

class EmployeeController extends BaseController
{
    /**
     * 获取员工列表
     * @param Request $request
     * @return Response
     */
    public function list(Request $request): Response
    {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 10);
        $keyword = $request->input('keyword', '');
        $partner_id = $request->input('partner_id', '');
        
        $query = Employee::query();
        
        if ($keyword) {
            $query->where(function ($q) use ($keyword) {
                $q->where('name', 'like', "%{$keyword}%")
                  ->orWhere('phone', 'like', "%{$keyword}%")
                  ->orWhere('id_card', 'like', "%{$keyword}%");
            });
        }
        
        if ($partner_id) {
            $query->where('partner_id', $partner_id);
        }
        
        $total = $query->count();
        $list = $query->with('partner')
                      ->offset(($page - 1) * $limit)
                      ->limit($limit)
                      ->orderBy('id', 'desc')
                      ->get();
        
        return $this->success([
            'list' => $list,
            'total' => $total
        ]);
    }

    /**
     * 添加员工
     * @param Request $request
     * @return Response
     */
    public function add(Request $request): Response
    {
        $data = $request->post();
        $rules = [
            'partner_id' => 'required|integer',
            'name' => 'required|string|max:50',
            'phone' => 'required|string|max:20',
            'id_card' => 'required|string|max:18',
            'position' => 'required|string|max:50',
            'status' => 'required|integer|in:0,1'
        ];
        
        $this->validate($data, $rules);
        
        // 检查手机号是否已存在
        if (Employee::where('phone', $data['phone'])->exists()) {
            return $this->error('该手机号已被使用');
        }
        
        // 检查身份证号是否已存在
        if (Employee::where('id_card', $data['id_card'])->exists()) {
            return $this->error('该身份证号已被使用');
        }
        
        $employee = new Employee();
        $employee->fill($data);
        $employee->save();
        
        return $this->success();
    }

    /**
     * 更新员工信息
     * @param Request $request
     * @return Response
     */
    public function edit(Request $request): Response
    {
        $id = $request->input('id');
        $data = $request->post();
        
        $rules = [
            'partner_id' => 'required|integer',
            'name' => 'required|string|max:50',
            'phone' => 'required|string|max:20',
            'id_card' => 'required|string|max:18',
            'position' => 'required|string|max:50',
            'status' => 'required|integer|in:0,1'
        ];
        
        $this->validate($data, $rules);
        
        $employee = Employee::find($id);
        if (!$employee) {
            return $this->error('员工不存在');
        }
        
        // 检查手机号是否已被其他员工使用
        if (Employee::where('phone', $data['phone'])->where('id', '!=', $id)->exists()) {
            return $this->error('该手机号已被使用');
        }
        
        // 检查身份证号是否已被其他员工使用
        if (Employee::where('id_card', $data['id_card'])->where('id', '!=', $id)->exists()) {
            return $this->error('该身份证号已被使用');
        }
        
        $employee->fill($data);
        $employee->save();
        
        return $this->success();
    }

    /**
     * 删除员工
     * @param Request $request
     * @return Response
     */
    public function delete(Request $request): Response
    {
        $id = $request->input('id');
        
        $employee = Employee::find($id);
        if (!$employee) {
            return $this->error('员工不存在');
        }
        
        $employee->delete();
        
        return $this->success();
    }

    /**
     * 更新员工状态
     * @param Request $request
     * @return Response
     */
    public function status(Request $request): Response
    {
        $id = $request->input('id');
        $status = $request->input('status');
        
        $employee = Employee::find($id);
        if (!$employee) {
            return $this->error('员工不存在');
        }
        
        $employee->status = $status;
        $employee->save();
        
        return $this->success();
    }
} 